Open In Colab WeaveはCohere Python libraryを介して行われたLLM呼び出しをweave.init()が呼び出された後に自動的に追跡およびログに記録します。

トレース

LLMアプリケーションのトレースを中央データベースに保存することは、開発中も本番環境でも重要です。これらのトレースはデバッグに使用し、アプリケーションの改善に役立つデータセットとしても活用できます。 Weaveは自動的にcohere-pythonのトレースをキャプチャします。通常通りライブラリを使用できます。まずweave.init()を呼び出します:
import cohere
import os
import weave

# Use the Cohere library as usual
co = cohere.Client(api_key=os.environ["COHERE_API_KEY"])

# highlight-next-line
weave.init("cohere_project")

response = co.chat(
    message="How is the weather in Boston?",
    # perform web search before answering the question. You can also use your own custom connector.
    connectors=[{"id": "web-search"}],
)
print(response.text)
cohereモデルの強力な機能の一つはconnectorsを使用することで、エンドポイント側で他のAPIにリクエストを行うことができます。レスポンスには、コネクタから返されたドキュメントにリンクする引用要素を含む生成テキストが含まれます。 cohere_trace.png
私たちはCohereClient.chatAsyncClient.chatClient.chat_stream、およびAsyncClient.chat_streamメソッドをパッチして、LLM呼び出しを追跡できるようにします。

独自のopsでラップする

Weave opsは結果をreproducibleにします。実験中にコードを自動的にバージョン管理し、入力と出力をキャプチャします。単に@weave.op()でデコレートされた関数を作成してCohereのチャットメソッドを呼び出すだけで、Weaveが入力と出力を追跡します。以下は例です:
import cohere
import os
import weave

co = cohere.Client(api_key=os.environ["COHERE_API_KEY"])

weave.init("cohere_project")

# highlight-next-line
@weave.op()
def weather(location: str, model: str) -> str:
    response = co.chat(
        model=model,
        message=f"How is the weather in {location}?",
        # perform web search before answering the question. You can also use your own custom connector.
        connectors=[{"id": "web-search"}],
    )
    return response.text

print(weather("Boston", "command"))
cohere_ops.png

Modelを作成して実験を容易にする

多くの要素が動いている場合、実験の整理は困難です。Modelクラスを使用することで、システムプロンプトや使用しているモデルなど、アプリの実験的な詳細をキャプチャして整理できます。これにより、アプリの異なるイテレーションを整理して比較するのに役立ちます。 コードのバージョン管理と入力/出力のキャプチャに加えて、Modelはアプリケーションの動作を制御する構造化されたパラメータをキャプチャし、どのパラメータが最も効果的だったかを簡単に見つけることができます。Weave Modelsはserve、およびEvaluationと一緒に使用することもできます。 以下の例では、modeltemperatureで実験できます。これらのいずれかを変更するたびに、新しいversionWeatherModelが得られます。
import weave
import cohere
import os

weave.init('weather-cohere')

class WeatherModel(weave.Model):
    model: str
    temperature: float
  
    @weave.op()
    def predict(self, location: str) -> str:
        co = cohere.Client(api_key=os.environ["COHERE_API_KEY"])
        response = co.chat(
            message=f"How is the weather in {location}?",
            model=self.model,
            temperature=self.temperature,
            connectors=[{"id": "web-search"}]
        )
        return response.text

weather_model = WeatherModel(
    model="command",
    temperature=0.7
)
result = weather_model.predict("Boston")
print(result)
cohere_model.png